//
//  LLMajorPicker.swift
//  LaiLang
//
//  Created by David on 2017/2/19.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

@objc protocol LLMajorPickerDelegate {
    @objc optional func majorPicker(_ picker: LLMajorPicker, selectedAt index: Int)
}

class LLMajorPicker: UIView, UIPickerViewDelegate, UIPickerViewDataSource {

    var data: [String]?
    
    var closeBtn: UIButton!
    
    var picker: UIPickerView!
    
    var delegate: LLMajorPickerDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        data = JZMajorList
        
        closeBtn = UIButton(type: .system)
        closeBtn.frame = CGRect(x: frame.width - 64, y: 8, width: 48, height: 20)
        closeBtn.setTitle("完成", for: .normal)
        closeBtn.addTarget(self, action: #selector(LLMajorPicker.hide), for: .touchUpInside)
        addSubview(closeBtn)
        
        picker = UIPickerView(frame: CGRect(x: 0, y: 44, width: frame.width, height: frame.height - 44))
        picker.delegate = self
        picker.dataSource = self
        addSubview(picker)
        
        alpha = 0
        backgroundColor = UIColor.white
    }
    
    convenience init(frame: CGRect, data: [String]?) {
        self.init(frame: frame)
        self.data = data
    }
    
    private var isShowed = false
    
    func show(){
        if !isShowed{
            center.y += frame.height
            alpha = 1
            UIView.animate(withDuration: 0.5, animations: { 
                self.center.y -= self.frame.height
            })
            isShowed = true
        }
    }
    
    func hide(){
        if isShowed{
            delegate?.majorPicker?(self, selectedAt: selectedIndex)
            alpha = 1
            UIView.animate(withDuration: 0.5, animations: { 
                self.center.y += self.frame.height
            }, completion: { (finished) in
                self.alpha = 0
                self.center.y -= self.frame.height
                
            })
            isShowed = false
        }
    }
    
    func toggleShowed(){
        if isShowed{
            hide()
        }else{
            show()
        }
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0{
            if data != nil {
                return data!.count + 1
            }
        }
        return 1
    }
    
    
    var selectedIndex: Int{
        get{
            return picker.selectedRow(inComponent: 0)
        }
        set(i){
            picker.selectRow(i, inComponent: 0, animated: true)
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0{
            if row == 0{
                return "请选择系别"
            }else{
                if data != nil{
                    return data![row - 1]
                }
                
            }
        }
        return nil
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
